package com.atguigu.custmer.service;

import com.atguigu.custmer.javabean.Customer;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/11 19:33
 * @注释
 */
public class CustomerService {
    //用来保存客户对象的数组
    private Customer[] cus;
    //记录已保存客户对象的数量
    private int total = 0;

    /**
     * 创建一个有参构造器，用来初始化costomer数组
     * @param length
     */
    public CustomerService(int length){
        cus = new Customer[length];
        Customer c1 = new Customer("张三", '男', 19, "15588546542", "zhangsan@guigu.com");
        Customer c2 = new Customer("李四", '男', 19, "15588546542", "zhangsan@guigu.com");
        Customer c3 = new Customer("王五", '男', 19, "15588546542", "zhangsan@guigu.com");
        addCustomer(c1);
        addCustomer(c2);
        addCustomer(c3);
    }

    /**
     * 增加客户
     * @param customer
     * @return
     */
    public boolean addCustomer(Customer customer){
        if (total >= cus.length){
            return false;
        }
        cus[total] = customer;
        total++;
        return true;
    }

    /**
     * 修改指定客户数据
     *
     * @param index
     * @return
     */
    public boolean replaceCustomer(int index, Customer customer){
        if (index < 0 && index >= total){
            return false;
        }

        cus[index] = customer;
        return true;
    }

    /**
     * 删除指定下标的客户信息
     * @param index
     * @return
     */
    public boolean deleteCustomer(int index){
        if (index < 0 || index >= total){
            return false;
        }
        for (int i = index; i < total - 1; i++){
            cus[i] = cus[i+1];
        }
        cus[total - 1] = null;
        total--;
        return true;
    }

    /**
     * 获取所有客户信息
     * @return
     */
    public Customer[] getAllCustomer(){
        Customer[] customers = new Customer[total];
        for (int i = 0; i < total; i++){
            customers[i] = cus[i];
        }
        return customers;
    }

    /**
     * 获取指定客户信息
     * @param index
     * @return
     */
    public Customer getCustomer(int index){
        if (index < 0 || index >= total){
            return null;
        }
        return cus[index];
    }
}
